/*
 * $Id: Timer.java 471756 2006-11-06 15:01:43Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.hezuti.common.utils;

/**
 * A bean that can be used to time execution of pages
 * 
 */
public class Timer {

	// Attributes ----------------------------------------------------
	long current = System.currentTimeMillis();

	long start = current;

	// Public --------------------------------------------------------
	/**
	 * 获取时间
	 */
	public long getTime() {
		// Return how long time has passed since last check point
		long now = System.currentTimeMillis();
		long time = now - current;

		// Reset so that next time we get from this point
		current = now;

		return time;
	}
	/**
	 * 总共时间
	 * 
	 * @return
	 */
	public long getTotal() {
		return System.currentTimeMillis() - start;
	}
	/**
	 * 时间字符串
	 * @return
	 */
	public String getTotalTime() {
		StringBuffer sb = new StringBuffer();
		long total = getTotal();
		int day = 0;
		int hour = 0;
		int minute = 0;
		int second = 0;
		int msecond = 0;

		msecond = (int) (total % 1000);
		total /= 1000;
		second = (int) (total % 60);
		total /= 60;
		minute = (int) (total % 60);
		total /= 60;
		hour = (int) (total % 24);
		total /= 24;
		day = (int) total;

		if (day > 0)
			sb.append(day).append("days ");
		if (hour > 0)
			sb.append(hour).append(":");
		if (minute > 0)
			sb.append(minute).append(":");
		if (second > 0)
			sb.append(second).append(":");
		if (msecond > 0)
			sb.append(msecond).append("'");

		return sb.toString();
	}
}
